﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MensUnoShop.Models;
using SenerPro;

namespace MensUnoShop.Filters
{
    /// <summary>
    /// 過濾賣場相關屬性，並建立賣場及當下類別物件
    /// </summary>
    public class RegionFreeFilter : ActionFilterAttribute
    {
        private Logger _log;

        public RegionFreeFilter()
        {
            _log = new Logger(Config.logpath);
        }

        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            int freeid;
            string tagid = filterContext.ActionParameters["c_1"] as string;
            string fid = filterContext.ActionParameters["c_2"] as string;
            List<Tag> navlist = new List<Tag>();
            Tag tag_c1 = TagFactory.GetTag(tagid);
            if (tag_c1 == null)
            {
                _log.RecordException(_log.FlowLogPath, "tag_c1 is not correct");
                filterContext.Result = new RedirectResult("/");
            }
            else
            {
                filterContext.Controller.ViewData["tag_c1"] = tag_c1;
                navlist.Add(tag_c1);
            }

            int.TryParse(fid, out freeid);
            Tag tag_c2 = TagFactory.GetTag(freeid);
            if (tag_c2 != null)
            {
                tag_c2.PARENTID = tag_c1.ID;
                filterContext.Controller.ViewData["tag_c2"] = tag_c2;
                navlist.Add(tag_c2);
            }
            filterContext.Controller.ViewData["navlist"] = navlist;
            base.OnActionExecuting(filterContext);
        }
    }
    
    

   

}